package com.mapbased.wfw.bind;

import com.mapbased.wfw.*;
import java.lang.reflect.InvocationTargetException;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class StaticBinding
    extends AbstractBinding
{
    /**
     * format : (12h)com.mapbased.test.SomeCalss.someStaticMethod.afterCaller.aftercaller
     * @param exp String
     */
    public StaticBinding(String exp)
    {
        super(exp);
    }

    protected void initMethods(String s)
    {
        //   String[] ss=s.split("\\.");
        int bi=0;
        for(int i=0;i < s.length();i++)
        {
            if(Character.isUpperCase(s.charAt(i)))
            {
                bi=i;
                break;
            }
        }
        int dotindex=s.indexOf('.',bi);
        String className=s.substring(0,dotindex);
        String exps=s.substring(dotindex + 1);
        String[] ss=exps.split("\\.");
        try
        {
            Class clz=Thread.currentThread().getContextClassLoader().loadClass(
                className);
            methodItems=new MethodItem[ss.length];
            for(int i=0;i < ss.length;i++)
            {
                MethodItem mi=new MethodItem();
                mi.name=ss[i].trim();
                methodItems[i]=mi;
            }
            //-----------------------------------------------------------
            MethodItem m0=methodItems[0];
            methodItems[0]=new MethodItem()
            {
                Object getValue(Object o)
                    throws SecurityException,SecurityException,
                    NoSuchMethodException,
                    InvocationTargetException,IllegalArgumentException,
                    IllegalAccessException
                {

                    return this.method.invoke(null,
                                              this.param.length == 0 ?
                                              this.param :
                                              new Object[]
                                              {o});
                }

            };
//---------------------------------------------------------------------------
            try
            {
                methodItems[0].method=clz.getMethod(m0.name,new Class[]
                    {RenderContext.class});
                methodItems[0].param=new Object[1];
            }

            catch(NoSuchMethodException ex1)
            {
                try
                {
                    methodItems[0].method=clz.getMethod(m0.name,new Class[]
                        {});
                    methodItems[0].param=new Object[0];
                }

                catch(NoSuchMethodException ex2)
                {
                    throw new java.lang.RuntimeException(
                        "Getting static method:" + exp,ex2);
                }

            }

            if(!methodItems[0].method.isAccessible())
            {
                methodItems[0].method.setAccessible(true);
            }

        }
        catch(ClassNotFoundException ex)
        {
            throw new java.lang.RuntimeException("Error parse:" + exp,ex);
        }

        // System.out.println(className);


    }

    public static void main(String[] ssfd)
    {
        new StaticBinding("(33h)com.mapbased.com.Render.sss");
    }

    /**
     * getDirectValue
     *this value is ignored because is  static method
     * but cannot return null because AbstractBinding return null directly
     *
     * @param context RenderContext
     * @return Object
     * @todo Implement this com.mapbased.wfw.bind.AbstractBinding method
     */
    protected Object getDirectValue(RenderContext context)
    {
        return context;
    }
}
